package com.neuedu.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.dao.UserDAO;
import com.neuedu.javabean.UserBean;
import com.neuedu.util.QueryUpdate;
import com.neuedu.util.ResultSetResult;

public class UserDAOImpl implements UserDAO{

	@Override
	public UserBean checkUname(String uname) {
		String sql="select * from t_user where uname=?";
		List<Object> objlist=QueryUpdate.query(sql,  new ResultSetResult() {

			@Override
			public Object getObjectByResultSet(ResultSet rs) throws SQLException {
				UserBean user=new UserBean();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				user.setUbirth(rs.getDate("ubirth"));
				user.setPwdquestion(rs.getString("pwdquestion"));
				user.setPwdanswer(rs.getString("pwdanswer"));
				user.setCreatetime(rs.getTimestamp("createtime"));
				user.setUpdatetime(rs.getTimestamp("updatetime"));
				user.setUlevel(rs.getInt("ulevel"));
				return user;
			}
			   
		   }, uname);
		if(objlist==null|| objlist.isEmpty()) {
			return null;
		}
		
		return (UserBean)objlist.get(0);
	}

	@Override
	public UserBean checkUpwd(String uname, String upwd) {
		
		String sql="select * from t_user where uname=? and upwd=?";
		List<Object> objlist=QueryUpdate.query(sql,  new ResultSetResult() {

			@Override
			public Object getObjectByResultSet(ResultSet rs) throws SQLException {
				UserBean user=new UserBean();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				user.setUbirth(rs.getDate("ubirth"));
				user.setPwdquestion(rs.getString("pwdquestion"));
				user.setPwdanswer(rs.getString("pwdanswer"));
				user.setCreatetime(rs.getTimestamp("createtime"));
				user.setUpdatetime(rs.getTimestamp("updatetime"));
				user.setUlevel(rs.getInt("ulevel"));
				return user;
			}
			   
		   }, uname,upwd);
		if(objlist==null|| objlist.isEmpty()) {
			return null;
		}
		
		return (UserBean)objlist.get(0);
	}

	@Override
	public int add(UserBean user) {
		String sql="insert into t_user(uname,upwd,ubirth,upwdquestion,upwdAnswer,ulevel)"
	           +" values(?,?,?,?,?,1)" ;
		return QueryUpdate.update(sql, user.getUname(),user.getUpwd(),user.getUbirth(),user.getPwdquestion(),user.getPwdanswer());
	}

	@Override
	public List<UserBean> findVip(int offset,int num) {
		
		String sql="select * from t_user where ulevel=1 limit ?,?";
		List<Object> objlist=QueryUpdate.query(sql,  new ResultSetResult() {

			@Override
			public Object getObjectByResultSet(ResultSet rs) throws SQLException {
				UserBean user=new UserBean();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				user.setUbirth(rs.getDate("ubirth"));
				user.setPwdquestion(rs.getString("pwdquestion"));
				user.setPwdanswer(rs.getString("pwdanswer"));
				user.setCreatetime(rs.getTimestamp("createtime"));
				user.setUpdatetime(rs.getTimestamp("updatetime"));
				user.setUlevel(rs.getInt("ulevel"));
				return user;
			}
			   
		   },offset,num);
		if(objlist==null|| objlist.isEmpty()) {
			return null;
		}
		List<UserBean> userlist=new ArrayList<>();
		for(Object obj:objlist) {
			userlist.add((UserBean)obj);
		}
		
		return userlist;
	}

	
	
	
	@Override
	public UserBean checkAnswer(String uname, String upwdquestion, String upwdAnswer) {
		String sql="select * from t_user where uname=? and pwdquestion=? and pwdAnswer=? ";
		List<Object> objlist=QueryUpdate.query(sql,  new ResultSetResult() {

			@Override
			public Object getObjectByResultSet(ResultSet rs) throws SQLException {
				UserBean user=new UserBean();
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpwd(rs.getString("upwd"));
				user.setUbirth(rs.getDate("ubirth"));
				user.setPwdquestion(rs.getString("pwdquestion"));
				user.setPwdanswer(rs.getString("pwdanswer"));
				user.setCreatetime(rs.getTimestamp("createtime"));
				user.setUpdatetime(rs.getTimestamp("updatetime"));
				user.setUlevel(rs.getInt("ulevel"));
				return user;
			}
			   
		   },uname,upwdquestion,upwdAnswer);
		if(objlist==null|| objlist.isEmpty()) {
			return null;
		}
		List<UserBean> userlist=new ArrayList<>();
		for(Object obj:objlist) {
			userlist.add((UserBean)obj);
		}
		
		return userlist.get(0);
	}

	@Override
	public int updateUpwd(String uname, String newupwd) {
		String sql="update t_user set upwd=? where uname=?";
		
		return QueryUpdate.update(sql, newupwd,uname);
	}

	@Override
	public Integer countsum() {
		String sql="select count(*) from t_user where ulevel=1";
		List<Object> listo=QueryUpdate.query(sql, new ResultSetResult() {
			
			@Override
			public Object getObjectByResultSet(ResultSet rs) throws SQLException {
				Integer count=rs.getInt(1);
				return count;
			}
		});
		if(listo==null||listo.isEmpty()) {
			return null;
		}
		return (Integer)listo.get(0);
	}

}
